home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / amiga / bmake15.lzh / main.c < prev    next >
C/C++ Source or Header  |  1991-11-03  |  5KB  |  252 lines

  1. /*    main.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  */
  5.  
  6. #include <ctype.h>
  7.  
  8. #include <exec/exec.h>
  9. #include <exec/execbase.h>
  10.  
  11. #include <intuition/intuitionbase.h>
  12. #include <graphics/gfxbase.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/dos_protos.h>
  16.  
  17. extern struct GfxBase *GfxBase;
  18. extern struct IntuitionBase *IntuitionBase;
  19.  
  20. #include "make.h"
  21. #include "depend.h"
  22.  
  23. /* Globals */
  24. struct globals Global = {
  25.     NULL,    /* me */
  26.     NULL,    /* logfile */
  27.     NULL,    /* screen */
  28.     NULL,    /* window */
  29.     NULL,    /* drinfo */
  30.  
  31.     {
  32.     (struct Node *)&Global.targetlist.lh_Tail,    /* lh_Head */
  33.     (struct Node *)NULL,                        /* lh_Tail */
  34.     (struct Node *)&Global.targetlist.lh_Head,    /* lh_TailPred */
  35.     (UBYTE)NT_USER,
  36.     (UBYTE)0
  37.     },    /* targetlist */
  38.     {
  39.     (struct Node *)&Global.speciallist.lh_Tail,    /* lh_Head */
  40.     (struct Node *)NULL,                        /* lh_Tail */
  41.     (struct Node *)&Global.speciallist.lh_Head,    /* lh_TailPred */
  42.     (UBYTE)NT_USER,
  43.     (UBYTE)0
  44.     },    /* speciallist */
  45.     {
  46.     (struct Node *)&Global.patternlist.lh_Tail,    /* lh_Head */
  47.     (struct Node *)NULL,                        /* lh_Tail */
  48.     (struct Node *)&Global.patternlist.lh_Head,    /* lh_TailPred */
  49.     (UBYTE)NT_USER,
  50.     (UBYTE)0
  51.     },    /* patternlist */
  52.     {
  53.     (struct Node *)&Global.macrolist.lh_Tail,    /* lh_Head */
  54.     (struct Node *)NULL,                        /* lh_Tail */
  55.     (struct Node *)&Global.macrolist.lh_Head,    /* lh_TailPred */
  56.     (UBYTE)NT_USER,
  57.     (UBYTE)0
  58.     },    /* macrolist */
  59.     0,    /* builtin flag */
  60.     0,    /* recursion level */
  61.     0L    /* oldcwd */
  62. };
  63.  
  64. static int
  65. open_libraries( void )
  66. {
  67. #ifndef _DCC
  68.     if( !( IntuitionBase = (struct IntuitionBase *)
  69.         OpenLibrary( "intuition.library", 33 ))) {
  70.         return( 1 );
  71.     }
  72.     if( !( GfxBase = (struct GfxBase *)
  73.         OpenLibrary( "graphics.library", 33 ))) {
  74.         return( 1 );
  75.     }
  76. #endif
  77.     return( 0 );
  78. }
  79.  
  80. static void
  81. close_libraries( void )
  82. {
  83. #ifndef _DCC
  84.     if( IntuitionBase )
  85.         CloseLibrary( IntuitionBase );
  86.  
  87.     if( GfxBase )
  88.         CloseLibrary( GfxBase );
  89. #endif
  90. }
  91.  
  92. static int
  93. new_globals( struct globals *globptr )
  94. {
  95.     globptr->me = (struct Process *) FindTask( NULL ); /* find this task */
  96.     return( 0 );
  97. death:
  98.     printf( "problem initializing globals\n" );
  99.     return( 1 );
  100. }
  101.  
  102. static int
  103. delete_globals( struct globals *globptr )
  104. {
  105.     if( Global.oldcwd ) {
  106.         UnLock( CurrentDir( Global.oldcwd ));
  107.     }
  108.  
  109.     memset( globptr, 0, sizeof(struct globals));
  110.  
  111.     /*    just allow exit() to take care of free()ing all of our
  112.      *    allocations, because I don't feel like doing it right now
  113.      */
  114.     NewList( &globptr->targetlist );
  115.     NewList( &globptr->speciallist );
  116.     NewList( &globptr->patternlist );
  117.     NewList( &globptr->macrolist );
  118.     return( 0 );
  119. }
  120.  
  121.  
  122. static void
  123. die( void )
  124. {
  125.     delete_params();
  126.  
  127.     close_logfile();
  128.  
  129.     delete_globals( &Global );
  130.     close_libraries();
  131. }
  132.  
  133. static int
  134. init( void )
  135. {
  136.     if( open_libraries())
  137.         goto death;
  138.     if( new_globals( &Global ))
  139.         goto death;
  140.  
  141.     return( 0 );
  142. death:
  143.     return( 1 );
  144. }
  145.  
  146. static long
  147. do_cl_macro( struct string_node *one )
  148. {
  149.     long retval;
  150.     int made;
  151.  
  152.     if( strchr( one->data, '=' )) {
  153.         process_macroline( one->data );
  154.         if( !Global.builtin_flag ) {
  155.             logprintf( "\t%s\n", one->data );
  156.             Remove( &one->node );
  157.             delete_snode( one );
  158.         }
  159.     }
  160.     return( 0 );
  161. }
  162.  
  163. static long
  164. run_one( struct string_node *one )
  165. {
  166.     long retval;
  167.     int made;
  168.  
  169.     debugprintf( 2, ("\n** run_one Make %s **\n", one->data ));
  170.     retval = (long) make_filename( one->data, &made );
  171.     if( !retval && !made ) {
  172.         logprintf( "\"%s\" is up to date\n", one->data );
  173.     }
  174.     return( retval );
  175. }
  176.  
  177. static void
  178. run_it( void )
  179. {
  180.     long retval;
  181.     int made;
  182.  
  183.     if( Param.filelist.lh_Head->ln_Succ )
  184.         (void)for_list( &Param.filelist, do_cl_macro );
  185.     if( Param.filelist.lh_Head->ln_Succ ) {
  186.         (void)for_list( &Param.filelist, run_one );
  187.     }
  188.     else {
  189.         struct target *first_goal;
  190.         for( first_goal = (struct target *)Global.targetlist.lh_Head;
  191.             first_goal->node.ln_Succ;
  192.             first_goal = (struct target *)first_goal->node.ln_Succ ) {
  193.             if( !(first_goal->flags & (TF_PATTERN|TF_BUILTIN )))
  194.                 break;
  195.         }
  196.         if( first_goal->node.ln_Succ ) {
  197.             debugprintf( 2, ("\n** first_goal Make %s **\n",
  198.                 first_goal->name ));
  199.             retval = make_filename( first_goal->name, &made );
  200.             if( !retval && !made ) {
  201.                 logprintf( "\"%s\" is up to date\n", first_goal->name );
  202.             }
  203.         }
  204.     }
  205.     logprintf( "\tMake done.\n" );
  206. }
  207.  
  208. int
  209. main( int argc, char *argv[] )
  210. {
  211. /*    Allow it to work with limitations on xsystem() and scdir()
  212.     if( SysBase->lib_Version < 36L ) {
  213.         printf( "This program requires Amiga OS 2.0\n" );
  214.         return( 20 );
  215.     }
  216. */
  217.     atexit( die );    /* add die() to normal exit procedure */
  218.  
  219.     if( parse_parameters( argc, argv ))
  220.         goto bailout;
  221.  
  222.     logprintf( "%s %s\n", version_string+7, verdate_string );
  223.     logprintf( "%s\n", copyright_string );
  224.  
  225.     if( init() )
  226.         goto bailout;
  227.     Global.builtin_flag = 1;
  228.     if( init_builtins())
  229.         goto bailout;
  230.     if( Param.filelist.lh_Head->ln_Succ )
  231.         (void)for_list( &Param.filelist, do_cl_macro );
  232.     Global.builtin_flag = 0;
  233.  
  234.     if( input_makefile( Param.makefile ))
  235.         goto bailout;
  236. #if DEBUG
  237.     if( Param.print_database )
  238.         dump_all();
  239.     else
  240.         run_it();
  241. #else
  242.     run_it();
  243. #endif
  244.  
  245.     return( 0 );
  246.  
  247. bailout:
  248.     /* die(); is called by exit */
  249.     return( 20 );
  250. }
  251.  
  252.